﻿using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace IllusionPlugin
{
	/// <summary>
	///     Create a New INI file to store or load data
	/// </summary>
	internal class IniFile
	{
		private string _path = "";

		public string Path
		{
			get => _path;
			set
			{
				if (!File.Exists(value))
					File.WriteAllText(value, "", Encoding.Unicode);
				_path = value;
			}
		}

		/// <summary>
		///     INIFile Constructor.
		/// </summary>
		/// <PARAM name="INIPath"></PARAM>
		public IniFile(string INIPath)
		{
			Path = INIPath;
		}

		[DllImport("KERNEL32.DLL", EntryPoint = "GetPrivateProfileStringW",
			SetLastError = true,
			CharSet = CharSet.Unicode, ExactSpelling = true,
			CallingConvention = CallingConvention.StdCall)]
		private static extern int GetPrivateProfileString(
			string lpSection,
			string lpKey,
			string lpDefault,
			StringBuilder lpReturnString,
			int nSize,
			string lpFileName);

		[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW",
			SetLastError = true,
			CharSet = CharSet.Unicode, ExactSpelling = true,
			CallingConvention = CallingConvention.StdCall)]
		private static extern int WritePrivateProfileString(
			string lpSection,
			string lpKey,
			string lpValue,
			string lpFileName);

		/// <summary>
		///     Write Data to the INI File
		/// </summary>
		/// <PARAM name="Section"></PARAM>
		/// Section name
		/// <PARAM name="Key"></PARAM>
		/// Key Name
		/// <PARAM name="Value"></PARAM>
		/// Value Name
		public void IniWriteValue(string Section, string Key, string Value)
		{
			WritePrivateProfileString(Section, Key, Value, Path);
		}

		/// <summary>
		///     Read Data Value From the Ini File
		/// </summary>
		/// <PARAM name="Section"></PARAM>
		/// <PARAM name="Key"></PARAM>
		/// <PARAM name="Path"></PARAM>
		/// <returns></returns>
		public string IniReadValue(string Section, string Key)
		{
			const int MAX_CHARS = 1023;
			var result = new StringBuilder(MAX_CHARS);
			GetPrivateProfileString(Section, Key, "", result, MAX_CHARS, Path);
			return result.ToString();
		}
	}
}